



//
//  ToolKit.m
//  nncloud
//
//  Created by macfai on 2018/11/30.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "ToolKit.h"

@implementation ToolKit


+(instancetype)shareInstance{
    
    static ToolKit *_toolClass = nil;
    static dispatch_once_t t;
    dispatch_once(&t,^{
        _toolClass = [[ToolKit alloc]init];
    });
    return _toolClass;
}


///时间戳转时间
+ (NSString *)getTimeFromTimesTamp:(NSString *)timeStr

{
    double time = [timeStr doubleValue]/1000;//java 默认时间精度是ms ,所以需要除以1000换成秒
    NSDate *myDate = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    //将时间转换为字符串
    NSString *timeS = [formatter stringFromDate:myDate];
    return timeS;
}


/**
 *   获取当前显示控制器
 *
 *  @return UIViewController
 */
+ (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}


- (NSString *)removeHTML:(NSString *)html {
    
    NSScanner *theScanner;
    
    NSString *text = nil;
    NSString *str = nil;
    
    theScanner = [NSScanner scannerWithString:html];
    
    while ([theScanner isAtEnd] == NO) {
        
        // find start of tag
        [theScanner scanUpToString:@"<" intoString:NULL] ;
        
        // find end of tag
        [theScanner scanUpToString:@">" intoString:&text] ;
        
        
        // replace the found tag with a space
        //(you can filter multi-spaces out later if you wish)
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""];
    }
    return html;
}

///验证合法手机号
+ (BOOL)validateMobile:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     * 中国电信：China Telecom
     * 133,1349,153,180,189
     */
    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    /**
     * 补充新号段:
     * （147、181、183、184、176、177、178、1700、1705、1709）
     */
    NSString *latestMobile = @"^1((47|8[134]|7[678])[0-9]|700|705|709)\\d{7}$";
    
    
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    BOOL reu1 = [regextestmobile evaluateWithObject:mobileNum];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    BOOL reu2 = [regextestcm evaluateWithObject:mobileNum];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    BOOL reu3 = [regextestcu evaluateWithObject:mobileNum];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    BOOL reu4 = [regextestct evaluateWithObject:mobileNum];
    
    NSPredicate *regextestlatest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", latestMobile];
    BOOL reu5 = [regextestlatest evaluateWithObject:mobileNum];
    
    if (reu1 || reu2 || reu3 || reu4 || reu5)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

//验证密码只支持数字、字母
+ (BOOL)judgePassWord:(NSString *)psd
{
    NSString *psdZZ = @"^[A-Za-z0-9]+$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", psdZZ];
    BOOL reu1 = [regextestmobile evaluateWithObject:psd];
    return reu1;
}

///判断身份证
+ (BOOL)validateIdCard:(NSString *)idCard//判断身份证格式
{
    NSString *psdZZ = @"^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|[X|x])$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", psdZZ];
    BOOL reu1 = [regextestmobile evaluateWithObject:idCard];
    
    return reu1;
}

///邮箱
+ (BOOL)validateMail:(NSString *)mail
{
    //   ^[A-Za-zd]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$
    NSString *emailCheck = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailCheck];
    return [emailTest evaluateWithObject:mail];
}

///判断是否为纯汉字
+ (BOOL)judgeChineseWord:(NSString *)text{
    
    NSString *psdZZ = @"[^\u4e00-\u9fa5]";
    //    NSString *psdZZ = @"^[1-9]/d*/./d*|0/./d*[1-9]/d*$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", psdZZ];
    BOOL reu1 = [regextestmobile evaluateWithObject:text];
    return reu1;
}



@end
